﻿using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;

namespace TST.SharePointObjects
{
    internal class FeatureDefinitionSiteReferences : ObjectReferenceList
    {
        public FeatureDefinitionSiteReferences(LicensingMode licensingMode)
            : base(licensingMode)
        {
        }

        public override ObjectReferenceType ObjectReferenceType
        {
            get { return ObjectReferenceType.FeatureDefinitionSite; }
        }

        internal void Add(SiteObjectList objects, SPWeb web, SPSite site, SPWebApplication webApplication)
        {
            foreach (SPFeature feature in site.Features)
            {
                SiteObject siteObject = objects.GetItemById(feature.DefinitionId.ToString("B"));
                if (siteObject != null && siteObject.Index)
                {
                    if (Globals.MatchingScope(siteObject, webApplication, site, web))
                    {
                        string refId = Globals.GetReferenceId(siteObject, feature);
                        ObjectReference newReference = new ObjectReference(site.ID.ToString("B"), String.Format("Site collection '{0}'.", web.Title), refId, ObjectReferenceType.FeatureDefinitionSite);
                        newReference.Url = string.Format("{0}/_layouts/ManageFeatures.aspx?Scope=Site", web.Url);
                        newReference.SiteTitle = web.Title;
                        newReference.SiteUrl = web.Url;
                        newReference.ListTitle = String.Empty;
                        newReference.ListUrl = String.Empty;
                        Add(newReference);
                    }
                }
            }
        }

    }
}
